﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace OCR.SystemThread
{
    class DIRecordThread
    {
        public static bool DIRecord_Flowrec = false;
        public static Thread DIRecordFlow_Thread = null;
        public static int iDIRecord_Step = 0;

        public static bool DIRecord_Stop = false;
        public static void DIRecord_Thread()
        {

            iDIRecord_Step = 0;
            DIRecord_Flowrec = true;
            DIRecordFlow_Thread = new Thread(DIRecordFlowThread);
            DIRecordFlow_Thread.IsBackground = true;
            DIRecordFlow_Thread.Start();


        }
        public static DateTime triggerDataTime;
        private static void DIRecordFlowThread(object obj)
        {
            while (DIRecord_Flowrec)
            {
                switch (iDIRecord_Step)
                {
                    case 0:
                        if (IOThread_Run.Di[0] == 1)
                        {
                            triggerDataTime = DateTime.Now;
                            if (!File.Exists("DI_Record_Result.txt"))
                            {
                                File.WriteAllText("DI_Record_Result.txt", null, Encoding.UTF8);
                            }
                            try
                            {
                                StreamWriter sw = File.AppendText("DI_Record_Result.txt");
                                string DI_Record_Result = "当前触发DI时间：" + triggerDataTime.ToString("yyyy:MM:dd:HH:mm:ss.fffffff");
                                Console.WriteLine(DI_Record_Result);
                                sw.WriteLine(DI_Record_Result);
                                sw.Close();
                                iDIRecord_Step = 5;
                            }
                            catch (Exception eX)
                            {

                                Console.WriteLine(eX.Message);
                            }
                        }
                        break;
                    case 5:
                        if (IOThread_Run.Di[0] == 0)
                        {
                            iDIRecord_Step = 0;
                        }
                        break;
                }
                Thread.Sleep(1);
            }
        }
    }
}
